<template>
  <van-button :color="btnColor" @click="handleClick">{{ btnText }}</van-button>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { showConfirmDialog, showFailToast, showSuccessToast } from "vant";
import QianzhikongApi from "@/api/qianzhikong-api";
import ConsultationConstant from "@/constant/consultation-constant";

export default defineComponent({
  name: "ConfirmBtn",
  components: {},
  props: {
    info: Object,
    btnText: {
      type: String,
      default: "确认",
    },
    btnColor: {
      type: String,
      default: "#f60",
    },
    approveParams: Object,
    goBackFunc: Function,
    confirmBeforePromise: Function,
    successToastMsg: {
      type: String,
      default: "成功",
    },
  },
  data() {
    return {
      price: null,
    };
  },
  computed: {},
  watch: {},
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {},
  beforeUpdate() {},
  updated() {},
  methods: {
    getApplyService() {
      const info = this.info;
      const serverInstiuttionUUID = info.serverInstiuttionUUID;
      const doctors = info.doctors;

      let serverInstitutionDepartmentIdAndDocTypeIdStr = "";
      if (doctors && doctors.length !== 0) {
        doctors.forEach((item) => {
          if (
            item.serverInstiuttionUUID === serverInstiuttionUUID ||
            info.approveStep === ConsultationConstant.approveStatus.diagnosis
          ) {
            serverInstitutionDepartmentIdAndDocTypeIdStr += `${item.department.uuid}/`;
            if (item.doctor.uuid != null) {
              serverInstitutionDepartmentIdAndDocTypeIdStr +=
                item.doctor.docTypeId;
            }
            serverInstitutionDepartmentIdAndDocTypeIdStr += ",";
          }
        });
      }

      if (info.id != null) {
        return QianzhikongApi.getApplyService({
          applyInstitutionId: info.applyInstitutionId,
          applyInstitutionDepartmentId: info.applyDepartmentId,
          serverInstiuttionUUID: serverInstiuttionUUID,
          consultatitionWay: info.consultationWay,
          consultationType: info.consultationType,
          serverInstitutionDepartmentIdAndDocTypeIdStr:
            serverInstitutionDepartmentIdAndDocTypeIdStr,
        });
      }

      return QianzhikongApi.getApplyService({
        serverInstiuttionUUID: serverInstiuttionUUID,
        consultatitionWay: info.consultationWay,
        consultationType: info.consultationType,
        serverInstitutionDepartmentIdAndDocTypeIdStr:
          serverInstitutionDepartmentIdAndDocTypeIdStr,
      });
    },
    async getPrice() {
      const info = this.info;
      if (info.consultationWay === "mdt_kuayuan") {
        // todo 多学科会诊逻辑
        return 0;
      }

      const price = await this.getApplyService();
      return price.entity.applyPrice;
    },
    async handleClick() {
      if (this.confirmBeforePromise) {
        await this.confirmBeforePromise();
      }

      const response = await QianzhikongApi.getCanSubmitApply({
        applyUUID: this.info.uuid,
      });

      const price = await this.getPrice();
      const info = this.info;
      if (!info.platformProxy && price === null) {
        showFailToast("未找到相应收费标准");
        return;
      }
      if (price > response.entity.money) {
        showFailToast(`账户余额不足(预估费用：${price}元)`);
        return;
      }
      if (!response.entity.enable) {
        showFailToast("该医院已被禁用");
        return;
      }

      showConfirmDialog({
        title: "确认",
        message: "您确认要审核通过吗？",
      }).then(() => {
        this.approveConsultationApply();
      });
    },
    approveConsultationApply() {
      QianzhikongApi.approveConsultationApply(this.info, {
        processAction: "agree",
        ...this.approveParams,
      }).then(() => {
        showSuccessToast({
          message: this.successToastMsg,
          duration: 2000,
          onClose: () => {
            if (this.goBackFunc) {
              this.goBackFunc();
              return;
            }
            this.$router.back();
          },
        });
      });
    },
  },
  beforeUnmount() {},
  unmounted() {},
});
</script>

<style scoped lang="scss"></style>
